<?php
class Channel{
    protected $main_table = 'data_channel';
	protected $main_table2 = 'data_channel_apply';
	protected $main_id = 'channel_id';
	protected $main_id2 = 'apply_id';
	protected $db = null;
	
	public function __construct($db){
		$this->db=$db;
	}
   
	public function __destruct(){
		unset($this->db);
	}
	public function get_table_name(){
	    return $this->main_table;
	    
	}
	public function get_table_name2(){
	    return $this->main_table2;
	    
	}
	/**
	* 	insert news
	* 	@param array $data 
	* 	@return int affected count
	*/
	public function insert_data($data){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$rows = $this->db->insert($this->main_table, $data);
		$insert_id = $this->db->getInsertId();
		return $insert_id;	
	}
	
	/**
	* 	update news
	* 	@param array $data 
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function update_data($data,$where){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$result = $this->db->update($this->main_table,$data,$where);
		return $result;
	}
	
	public function update_apply_data($data,$where){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$result = $this->db->update($this->main_table2,$data,$where);
		return $result;
	}
	
	/**
	* 	delete news
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function del_data($where){
		$result = $this->db->delete($this->main_table,$where);
		return $result;
	}
	
	/**
	* 	delete photos
	* 	@param string $photos 
	*/
	public function del_photo($photos){
		if($photos){
			$array = explode(',',$photos);
			foreach($array as $photo){
				$file = UPLOAD_PHOTO.$photo;
				$file2 = UPLOAD_PHOTO.'thumb/'.$photo;
				del_file($file);
				del_file($file2);
			}
		}
	}
	
	
	
	/**
	* 	get news info
	* 	@param int $id 
	*	@param array/string $field
	* 	@return array one row data
	*/
	public function get_data_row($id,$field='*'){
		if(!intval($id))
		my_jump('输入的参数有误!');
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$this->main_id."='".intval($id)."' ";
		$data = $this->db->getRow($sql);
		return $data;
	}
	
	public function get_data_by_sn($sn,$field='*'){
		if(!$sn)
		my_jump('输入的参数有误!');
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE channel_sn ='".$sn."' ";
		$data = $this->db->getRow($sql);
		return $data;
	}
	
	
	public function get_apply_data_row($id){
		if(!intval($id))
		my_jump('输入的参数有误!');
		$sql = "SELECT a.*,b.user_bank_name,b.user_true_name,b.user_bank_account FROM ".$this->main_table2." a left join data_user b on (a.apply_uid = b.user_id) WHERE ".$this->main_id2."='".intval($id)."' ";
		$data = $this->db->getRow($sql);
		return $data;
	}
	
	/**
	* 	get news array
	* 	@param string $where
	* 	@return array data
	*/
	public function get_data_array($where,$order='channel_id DESC',$limit='10',$field){
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$where." ORDER BY ".$order;
		if(intval($limit)){
			$sql .= " LIMIT ".$limit;
		}
		$data = $this->db->getAll($sql);
		return $data;
	}  

	//各渠道的安装数
	public function get_install_count($sn){
		$sql = "SELECT count(*) as num,imei_channel_sn FROM `data_imei` WHERE imei_channel_sn IN (".$sn.") GROUP BY imei_channel_sn";
		//$this->db->debug(1,1);
        $data = $this->db->getAll($sql);
		return $data;	
	
	}
	//注册用户数
	public function get_reg_count($sn){	
		$sql = "SELECT count(*) as num,imei_channel_sn FROM `data_imei` WHERE imei_channel_sn IN (".$sn.") and imei_uid>'0' GROUP BY imei_channel_sn";
		$data = $this->db->getAll($sql);
		return $data;	
	
	}
	//用户激活数
	public function get_active_count($sn){
		$sql = "SELECT count(*) as num,imei_channel_sn FROM `data_imei` WHERE imei_channel_sn IN (".$sn.") and imei_reg_phone>''  GROUP BY imei_channel_sn";
		$data = $this->db->getAll($sql);
		return $data;	
	
	}
	
	
	public function search_user($key){
		$sql = "SELECT user_id,user_name FROM `data_user` WHERE user_name LIKE '%".$key."%'";
		$data = $this->db->getAll($sql);
		return $data;	
	
	}
	
	
	public function check_sn($sn){
		$sql = "SELECT channel_sn FROM ".$this->main_table." WHERE channel_sn = '".$sn."' ";		
		$row = $this->db->getOne($sql);
		if($row){
			my_jump('很遗憾,渠道号:'.$sn.'已经存在!');			
		}		
	}   
	
	public function get_channel($id){
		$sql = "SELECT * FROM data_channel WHERE channel_id = '".$id."' ";		
		$row = $this->db->getRow($sql);
		//dump($row);
		return $row;
				
	}
	
	public function get_apply_info($id){
		$sql="SELECT apply_user,apply_uid,apply_phone FROM data_channel_apply WHERE apply_id = '".$id."'";
		$row = $this->db->getRow($sql);
		return $row;	
	}	

	public function insert_apply_data($data){
		if (!is_array($data))
	    return false;
		$rows = $this->db->insert('data_channel_apply', $data);
		$insert_id = $this->db->getInsertId();
		return $insert_id;
	}
	
	public function get_apply_list($uid){
		if(!$uid) my_jump('参数错误!');
		$sql = "SELECT a.*,b.channel_apk_url,b.channel_code_img,b.channel_sn FROM data_channel_apply as a left join data_channel as b on a.apply_id = b.channel_apply_id  WHERE apply_uid = '$uid' ORDER BY apply_id desc";
		$data = $this->db->getAll($sql);
		if($data){
			foreach($data as $k=>$v){
				
				
			}
			
		}
		return $data;
			
	}
	
	 /**
     * 	check channel_apply 
     * 	@param string $uid
     * 	@return boolean value
     */
	 public function check_channel_apply($uid){
		$sql = "SELECT apply_id FROM " . $this->main_table2 . " WHERE apply_uid = '$uid' and apply_status = '0' ";		
		$result = $this->db->getOne($sql);
		if($result){
			return true;
		}else{
			return false;
		}
		
	}
}

?>